//
//  TJChainNameController.m
//  Teleport
//
//  Created by botu　 on 2020/8/7.
//  Copyright © 2020 byx. All rights reserved.
//

#import "TJChainNameController.h"
#import "TJChainNameView.h"
@interface TJChainNameController ()

@property (strong, nonatomic) NSArray *datas;

@property(nonatomic,strong)NSIndexPath *lastPath;

@property (assign, nonatomic) BOOL isFist;
@end

@implementation TJChainNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    /** 帮助按钮**/
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithImageName:@"tip_icon" target:self action:@selector(didClickRightButton)];
    self.navigationBar.title = @"链名称";
    self.isFist = YES;
    
    self.tableView.rowHeight = 49;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:style1Cell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(KNavBarHight);
    }];
    
    self.datas = @[@"USDT-OMINI",@"USDT-ERC20"];
    if ([self.chainName isEqualToString:@"USDT-ERC20"]) {
          NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:0];
          [self tableView:self.tableView didSelectRowAtIndexPath:path];
    }else{
        NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
          [self tableView:self.tableView didSelectRowAtIndexPath:path];
    }
  
    
    
    
}

- (void)didClickRightButton {
    [TJChainNameView show];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:style1Cell forIndexPath:indexPath];
    NSInteger row = [indexPath row];
    
    NSInteger oldRow = [self.lastPath row];
    
    if (row == oldRow && self.lastPath!=nil) {
        
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
    }else{
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    }
    cell.textLabel.text = self.datas[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger newRow = [indexPath row];
    
    NSInteger oldRow = (self.lastPath !=nil)?[self.lastPath row]:-1;
    
    if (newRow != oldRow) {
        
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastPath];
        
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        self.lastPath = indexPath;
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.isFist = !self.isFist;
    
    
    if (!self.isFist)return;
    NSString *chainName = self.datas[indexPath.row];
    [self.navigationController qmui_popViewControllerAnimated:YES completion:^{
        if (self.complete) {
            self.complete(chainName);
        }
    }];
    
    
    
    
}
@end
